<?php
require_once 'core/config.php';

try {
    $dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_CHARSET;
    $pdo = new PDO($dsn, DB_USER, DB_PASS);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "开始初始化数据库...\n";
    
    // 删除旧表（如果存在）
    $pdo->exec("DROP TABLE IF EXISTS plugin_categories");
    $pdo->exec("DROP TABLE IF EXISTS plugins");
    $pdo->exec("DROP TABLE IF EXISTS categories");
    $pdo->exec("DROP TABLE IF EXISTS admin_logs");
    $pdo->exec("DROP TABLE IF EXISTS access_logs");
    
    echo "已删除旧表\n";
    
    // 创建插件表
    $pdo->exec("CREATE TABLE plugins (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100) UNIQUE NOT NULL,
        title VARCHAR(200) NOT NULL,
        description TEXT,
        version VARCHAR(50),
        author VARCHAR(100),
        icon VARCHAR(200),
        is_active TINYINT(1) DEFAULT 1,
        view_count INT DEFAULT 0,
        like_count INT DEFAULT 0,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        INDEX idx_name (name),
        INDEX idx_active (is_active),
        INDEX idx_views (view_count)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    
    echo "已创建 plugins 表\n";
    
    // 创建分类表
    $pdo->exec("CREATE TABLE categories (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100) UNIQUE NOT NULL,
        description TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_name (name)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    
    echo "已创建 categories 表\n";
    
    // 创建插件分类关联表
    $pdo->exec("CREATE TABLE plugin_categories (
        id INT AUTO_INCREMENT PRIMARY KEY,
        plugin_name VARCHAR(100) NOT NULL,
        category_id INT NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE KEY unique_plugin_category (plugin_name, category_id),
        FOREIGN KEY (plugin_name) REFERENCES plugins(name) ON DELETE CASCADE,
        FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE,
        INDEX idx_plugin (plugin_name),
        INDEX idx_category (category_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    
    echo "已创建 plugin_categories 表\n";
    
    // 创建操作日志表
    $pdo->exec("CREATE TABLE admin_logs (
        id INT AUTO_INCREMENT PRIMARY KEY,
        action VARCHAR(100) NOT NULL,
        target TEXT,
        admin_user VARCHAR(50) DEFAULT 'admin',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_action (action),
        INDEX idx_created_at (created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    
    echo "已创建 admin_logs 表\n";
    
    // 创建访问日志表
    $pdo->exec("CREATE TABLE access_logs (
        id INT AUTO_INCREMENT PRIMARY KEY,
        ip VARCHAR(45),
        user_agent TEXT,
        url TEXT,
        referer TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_ip (ip),
        INDEX idx_created_at (created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
    
    echo "已创建 access_logs 表\n";
    
    // 插入一些默认分类
    $defaultCategories = [
        ['name' => '开发工具', 'description' => '程序员开发必备工具'],
        ['name' => '格式转换', 'description' => '各种格式转换工具'],
        ['name' => '加密解密', 'description' => '数据加密解密工具'],
        ['name' => '图片处理', 'description' => '图片编辑和处理工具'],
        ['name' => '文本处理', 'description' => '文本编辑和处理工具'],
        ['name' => '网络工具', 'description' => '网络相关工具'],
        ['name' => '计算工具', 'description' => '各种计算器工具']
    ];
    
    $stmt = $pdo->prepare("INSERT INTO categories (name, description) VALUES (?, ?)");
    foreach ($defaultCategories as $category) {
        $stmt->execute([$category['name'], $category['description']]);
    }
    
    echo "已插入默认分类\n";
    
    echo "数据库初始化完成！\n";
    echo "现在可以访问网站了。\n";
    
} catch (PDOException $e) {
    echo "数据库初始化失败: " . $e->getMessage() . "\n";
}
?> 